<template>
  <div
    class="wrap-item"
    :style="{
      '--scroll-width': '175px',
    }"
  >
    <!-- 搜索-->
    <el-form ref="formSearch" :model="formSearch" size="small" inline>
      <el-form-item>
        <el-input
          v-model.trim="formSearch.param.name"
          placeholder="请输入工单名称"
          maxlength="20"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-select
          clearable
          v-model="formSearch.param.status"
          placeholder="请选择状态"
        >
          <el-option label="启用" value="0"></el-option>
          <el-option label="停用" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select
          clearable
          v-model="formSearch.param.workType"
          placeholder="请选择类别"
        >
          <el-option label="流程类" value="0"></el-option>
          <el-option label="指派类" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-cascader
          style="width: 100%"
          v-model="formSearch.param.formTypeId"
          :options="typeData"
          @change="handleChange"
          :props="{
            value: 'id',
            label: 'typeName',
            children: 'childList',
            expandTrigger: 'hover',
            emitPath: false,
            checkStrictly: true,
          }"
          :checkStrictly="true"
          :show-all-levels="false"
          placeholder="请选择工单类型"
          ref="typeCascader"
          clearable
          popper-class="theder"
        >
        </el-cascader>
      </el-form-item>
      <el-form-item>
        <span class="wrap-btn wrap-primary" @click="search">查询</span>
        <span
          class="wrap-btn wrap-success"
          v-if="$checkPermission(101)"
          @click="add"
          >添加工单</span
        >
      </el-form-item>
    </el-form>
    <!-- 数据列表 -->
    <div class="wrap-other-roll">
      <el-table
        :data="tableData"
        :header-cell-style="{ background: '#F6F7F9' }"
      >
        <el-table-column
          type="index"
          width="50"
          label="序号"
          align="center"
        ></el-table-column>
        <el-table-column prop="name" label="工单名称"></el-table-column>
        <el-table-column label="工单类别">
          <template slot-scope="scope">
            <span v-if="scope.row.workType == 0">流程类</span>
            <span v-if="scope.row.workType == 1">指派类</span>
          </template>
        </el-table-column>
        <el-table-column prop="formTypeId" label="所属类型"></el-table-column>
        <el-table-column prop="addName" label="创建人"></el-table-column>
        <el-table-column prop="addTime" label="创建时间"
          ><template slot-scope="scope">{{
            scope.row.addTime
          }}</template></el-table-column
        >
        <el-table-column prop="updateTime" label="最后更新时间"
          ><template slot-scope="scope">{{
            scope.row.updateTime
          }}</template></el-table-column
        >
        <el-table-column label="状态" width="80px" align="center">
          <template slot-scope="scope">
            <el-link
              type="success"
              :underline="false"
              v-if="scope.row.status == 0"
              >启用</el-link
            >
            <el-link
              type="danger"
              :underline="false"
              v-if="scope.row.status == 1"
              >停用</el-link
            >
          </template>
        </el-table-column>
        <el-table-column
          prop="content"
          label="操作"
          width="80px"
          align="center"
        >
          <template slot-scope="scope">
            <el-dropdown @command="(command) => handle(command, scope.row)">
              <span class="el-dropdown-link">
                更多
                <i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="edit" v-if="$checkPermission(102)"
                  >编辑</el-dropdown-item
                >
                <el-dropdown-item
                  command="changeStatus"
                  v-if="scope.row.status == 1 && $checkPermission(105)"
                  >启用</el-dropdown-item
                >
                <el-dropdown-item
                  command="changeStatus"
                  v-if="scope.row.status == 0 && $checkPermission(106)"
                  >停用</el-dropdown-item
                >
                <el-dropdown-item command="remove" v-if="$checkPermission(103)"
                  >删除</el-dropdown-item
                >
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <web-page
      :page="formSearch.pageNum"
      :size="formSearch.pageSize"
      :total="total"
      @sizeChange="sizeChange"
      @currentChange="currentChange"
    ></web-page>
  </div>
</template>

<script>
export default {
  data() {
    return {
      typeData: [],
      formSearch: {
        pageNum: 1,
        pageSize: 10,
        param: {},
      },
      total: null,
      tableData: [],
    }
  },
  created() {
    this.typeList()
    this.itemList()
  },
  methods: {
    itemList() {
      // 获取列表
      this.$axios
        .post('/workorder/setting/page', this.formSearch)
        .then((data) => {
          this.tableData = []
          if (data.code == 200) {
            this.tableData = data.data.list
            this.total = data.data.total
          } else {
            this.$message.error(data.msg)
          }
        })
    },
    sizeChange(val) {
      //每页条数
      this.formSearch.pageSize = val
      this.itemList()
    },
    currentChange(val) {
      //分页
      this.formSearch.pageNum = val
      this.itemList()
    },
    search() {
      //搜索
      this.formSearch.pageNum = 1
      this.itemList()
    },
    add() {
      //添加
      this.$router.push('/orderCenter/handle')
    },
    handle(command, row) {
      //操作
      if (!command) {
        this.$message.error('菜单选项缺少command属性')
        return
      }
      switch (command) {
        case 'edit':
          this.$router.push({
            path: '/orderCenter/handle',
            query: { id: row.id },
          })
          break
        case 'changeStatus':
          //启用停用
          const status = row.status == 0 ? 1 : 0
          this.$axios
            .get(
              '/workorder/setting/updateStatus?id=' +
                row.id +
                '&status=' +
                status
            )
            .then((data) => {
              if (data.code == 200) {
                this.itemList()
                this.$message.success('操作成功！')
              } else {
                this.$message.error(data.msg)
              }
            })
          break
        case 'remove':
          //删除
          this.$confirm('您确定要删除该条数据吗？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          })
            .then(() => {
              this.$axios
                .get('/workorder/setting/del?id=' + row.id)
                .then((data) => {
                  if (data.code == 200) {
                    this.itemList()
                    this.$message.success('操作成功！')
                  } else {
                    this.$message.error(data.msg)
                  }
                })
            })
            .catch(() => {})
          break
      }
    },
    save() {
      //保存
      if (!this.form.userName) {
        this.$message.error('请输入姓名')
        return
      }
      if (!isPhone(this.form.phone)) {
        this.$message.error('请输入正确的手机号')
        return
      }
      if (this.form.groupId.length == 0) {
        this.$message.error('请选择工单分组')
        return
      }
      const dept = this.$refs.tree.getCheckedNodes()
      if (dept.length == 0) {
        this.$message.error('请选择组织机构')
        return
      }
      this.form.orgIds = []
      dept.forEach((element) => {
        this.form.orgIds.push({
          id: element.id,
          orgName: element.orgName,
        })
      })
      if (this.form.roleIds.length == 0) {
        this.$message.error('请选择角色')
        return
      }
      this.$axios.post('/admin/user/edit', this.form).then((data) => {
        if (data.code == 200) {
          this.visible = false
          this.$message.success('操作成功！')
          this.itemList()
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    typeList() {
      // 获取工单类型
      this.$axios
        .post('/workorder/configWorkOrderTypeManage/workTypeList')
        .then((data) => {
          this.typeData = []
          if (data.code == 200) {
            this.typeData = data.data
          } else {
            this.$message.error(data.msg)
          }
        })
    },
    handleChange(val) {
      this.$refs.typeCascader.dropDownVisible = false //监听值发生变化就关闭它
    },
  },
}
</script>
